{
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "import xlwings as xw\n",
        "import pandas as pd\n",
        "import numpy as np"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {},
      "id": "8e773469-72bf-4249-a0e0-fc101f5556a9"
    },
    {
      "cell_type": "code",
      "source": [
        "# 读取要追加的行数据，以列表嵌套\n",
        "df_rows = pd.read_excel('./excel_output.xls',header=0,usecols=[1,2,3,4,5,6,7,8,9])\n",
        "list_rows = np.array(df_rows).tolist()"
      ],
      "outputs": [],
      "execution_count": 2,
      "metadata": {},
      "id": "ca2c1e06-4bc6-44fe-83da-197cc7c3a836"
    },
    {
      "cell_type": "code",
      "source": [
        "# 追加数据\n",
        "def append_data(list_rows,placeholder='A1',num=1):\n",
        "    app = xw.apps.add()\n",
        "    workbook = app.books.open('./销售跟踪try.xlsx')\n",
        "    for i,n in zip(workbook.sheets,range(len(list_rows ))):\n",
        "        worksheet = workbook.sheets[i]\n",
        "        values = worksheet.range(placeholder).expand()\n",
        "        number = values.shape[0]\n",
        "        worksheet.range(number + 1,num).value = list_rows[n]\n",
        "        workbook.save()\n",
        "    workbook.close()\n",
        "    app.quit()\n",
        "    print('Done !!')"
      ],
      "outputs": [],
      "execution_count": 3,
      "metadata": {},
      "id": "6a9b0900-893f-44a6-be53-73c59134ecdd"
    },
    {
      "cell_type": "code",
      "source": [
        "placeholder = 'C1'\n",
        "num = 3\n",
        "append_data(list_rows,placeholder,num)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Done !!\n"
          ]
        }
      ],
      "execution_count": 4,
      "metadata": {},
      "id": "fb864d5c-8da5-44ba-b1e5-8a78d800dca1"
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.6"
    },
    "nteract": {
      "version": "0.28.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}